import * as module from "@/tools/util";
import * as Request from '@/api/liubike';
import store from "@/tools/factory"
import { config, isDev } from '@/api/config';

import wx  from '@/tools/wx'
import commonJs from '@/views/commonJs/common'
let that
let loginInfo = null;

/**
 * 转化:   that.data -> that._data
 * ev.detail -> ev.target
 */

export default {
    mixins: [commonJs],
    data() {
        return {
            title: "加载中...", // 状态
            list: [], // 数据列表
            type: "", // 数据类型
            pageConfig: {
                offset: 0,
                limit: 20
            },
            footerVisbale: false,
            loading: true // 显示等待框
        }
    },
    mounted() {
        that = this;
        this.getInfo();
    },
    methods: {
        onLoad(options) {
            that = this;
            this.getInfo();
        },
        getInfo(callback) { // 获取优惠券列表
            let APPDATA = that._data;
            wx.showLoading({
                title: '加载中...',
            })
            Request.getWallentdetail(
                APPDATA.pageConfig
            ).then(res => {
                wx.hideLoading();
                let req = res.data;
                if (res.success) {
                    if (req.length == 0) {
                        that.setData({
                            footerVisbale: true
                        })
                        return;
                    }
                    if (req.length < 20 && APPDATA.pageConfig.offset == 0) {
                        that.setData({
                            footerVisbale: true
                        })
                    }
                    req.forEach((item, index) => {
                        if (!item.credit) {
                            item.totalMoney = (
                                item.money +
                                item.givemoney +
                                item.returnmoney
                            ).toFixed(2);
                        } else {
                            item.totalMoney = (
                                item.money +
                                item.givemoney +
                                item.returnmoney +
                                item.credit
                            ).toFixed(2);
                            item.wallent = (
                                item.money +
                                item.givemoney +
                                item.returnmoney
                            ).toFixed(2);
                        }
                        item.txt = that.typeToStr(item.type, item.totalMoney, item.customno);
                        let date = new Date(item.createdAt);
                        item.createdAt = that.fomatDate(date, "yyyy.MM.dd hh:mm");
                    });
                    that.setData({
                        list: that._data.list.concat(req)
                    })
                } else {
                    if (res.message == '暂无明细') {
                        that.setData({
                            footerVisbale: true
                        })
                    }
                    wx.showToast({
                        title: res.message,
                        icon: 'none',
                        duration: 2000
                    })
                }
            }).catch(err => {
                wx.hideLoading();
                wx.showToast({
                    title: '小遛需要缓缓',
                    icon: 'none',
                    duration: 2000
                })

            }).finally(res=> {
                callback && callback()
            });
        },
        fomatDate(date, fmt) {
            if (/(y+)/.test(fmt)) {
                fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
            }
            let o = {
                'M+': date.getMonth() + 1,
                'd+': date.getDate(),
                'h+': date.getHours(),
                'm+': date.getMinutes(),
                's+': date.getSeconds()
            };
            for (let k in o) {
                if (new RegExp(`(${k})`).test(fmt)) {
                    let str = o[k] + '';
                    fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : that.padLeftZero(str));
                }
            }
            return fmt;
        },
        typeToStr(type, givemoney, customno) {
            switch (type + "") {
                case "0":
                    return "骑行扣费";
                case "1":
                    return "充值成功";
                case "2":
                    return "人工修改";
                case "3":
                    return "推荐奖励";
                case "4":
                    if (givemoney < 0) {
                        return `${customno}期领养清空骑行费用`;
                    } else {
                        return `${customno}期领养活动返还`;
                    }
                case "5":
                    return "退款成功";
                case "6":
                    return "兑换卡券";
                case "7":
                    return "违停扣费";
                case "8":
                    return "违停扣费返还";
                case "9":
                    return "平账";
                case "10":
                    return "返还骑行费用";
                case "11":
                    return "骑行奖励";
                case "12":
                    return "违停扣费";
                case "13":
                    return "违停扣费返还";
                case "14":
                    return "退余额";
                default:
                    return "";
            }
        },
        padLeftZero(str) {
            return ('00' + str).substr(str.length);
        },
        onShareAppMessage: function () {
            return {
                title: '小遛共享',
                path: '/pages/index/index',
            }
        },
        onReachBottom(callback) {
            console.log('skskssssssss')
            // debugger
            if (that._data.footerVisbale == true) {
                return false;
            }

            that.setData({
                'pageConfig.offset': that._data.pageConfig.offset + 20
            })
            console.log(callback, 'callback')
            that.getInfo(callback);
        }
    }
}
